/**
 * https://leetcode.cn/problems/coin-change/submissions/550678997/
 * 322.零钱兑换
 * medium 2024.07.29
 * dp
 */

class Solution {
public:
    int coinChange(vector<int>& coins, int amount) {
        int n=coins.size();
        vector<int> dp(amount+1,-1);
        dp[0]=0;
        for(int i=1;i<=amount;++i){
            for(int j=0;j<coins.size();++j){
                if(coins[j]<=i&&dp[i-coins[j]]!=-1)
                    if(dp[i]==-1||dp[i]>dp[i-coins[j]]+1)
                    dp[i]=dp[i-coins[j]]+1;
        }
        }
        return dp[amount];
    }
};